package com.taojin.shoppinginterest.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.alibaba.fastjson.JSON;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.bitmap.PauseOnScrollListener;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.taojin.shoppinginterest.R;
import com.taojin.shoppinginterest.activity.SpecialDetailActivity;
import com.taojin.shoppinginterest.adapter.SubjectAdapter;
import com.taojin.shoppinginterest.bean.Special;
import com.taojin.shoppinginterest.bean.SpecialList;
import com.taojin.shoppinginterest.bean.Sub;
import com.taojin.shoppinginterest.constants.ApiConfig;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by T400 on 15-5-11.
 */
public class SubjectFragment extends Fragment implements PullToRefreshBase.OnRefreshListener2,AdapterView.OnItemClickListener {

    @ViewInject(R.id.fragment_home_special_PulltoRefreshListView)
    private PullToRefreshListView pullToRefreshListView;
    private SubjectAdapter subjectAdapter;
    private List<SpecialList> listAll;
    private HttpUtils httpUtils;
    private BitmapUtils bitmapUtils;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home_special, null);
        ViewUtils.inject(this, view);
        initView();
        loadData();
        pullToRefreshListView.setOnItemClickListener(this);
        return view;
    }

    public void initView() {
        listAll = new ArrayList<>();
        subjectAdapter = new SubjectAdapter(listAll, getActivity());
        httpUtils = new HttpUtils();
        bitmapUtils = new BitmapUtils(getActivity());

        pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
        pullToRefreshListView.setOnRefreshListener(this);
        pullToRefreshListView.setOnScrollListener(new PauseOnScrollListener(bitmapUtils, true, false));
        pullToRefreshListView.setAdapter(subjectAdapter);

    }

    public RequestParams getRequestParams() {
        RequestParams requestParams = new RequestParams();
        requestParams.addBodyParameter("act", "lists");
        requestParams.addBodyParameter("app", "topic");
        requestParams.addBodyParameter("page", "1");
        requestParams.addBodyParameter("perpage", "20");
        requestParams.addBodyParameter("pt", "2");
        requestParams.addBodyParameter("ver", "1.1");
        return requestParams;
    }

    public RequestParams getRequestParmsDetail(){
        RequestParams requestParams = new RequestParams();
        requestParams.addBodyParameter("act", "detail");
        requestParams.addBodyParameter("app", "topic");
        requestParams.addBodyParameter("id", "326");
        requestParams.addBodyParameter("pt", "2");
        requestParams.addBodyParameter("ver", "1.1");
        return requestParams;
    }


    public void loadData() {
        httpUtils.send(HttpRequest.HttpMethod.POST, ApiConfig.URL, getRequestParams(), new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String data = responseInfo.result;
                Special special = JSON.parseObject(data, Special.class);
                if (special != null) {
                    if (special.getData() != null) {
                        if (special.getData().getList() != null && !special.getData().getList().isEmpty()) {
                            listAll.clear();
                            listAll.addAll(special.getData().getList());
                            subjectAdapter.notifyDataSetChanged();
                        }
                    }
                }
                pullToRefreshListView.onRefreshComplete();
            }
            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
    }



    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        loadData();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        loadData();
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        List<Sub> list=listAll.get(position-1).getSub();
        String urlId=list.get(list.size()-1).getId();
        String topicUrl=ApiConfig.getTopicUrl(urlId);

        Intent intent=new Intent(getActivity(),SpecialDetailActivity.class);
        Bundle bundle=new Bundle();
        bundle.putString("URL", topicUrl);
        intent.putExtras(bundle);
        startActivity(intent);

    }
}
